﻿using Abp.EntityFrameworkCore;
using HCD.Entities;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace HCD.EntityFrameworkCore
{
    public class HisDbContext : AbpDbContext
    {
        public HisDbContext(DbContextOptions<HisDbContext> options)
            : base(options)
        {
            //更新数据库
            // if (Database.GetPendingMigrations().Any())
            // {
            //     Database.Migrate();
            // }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<PatsDiagnosisV>().ToView("V_HCD_PATS_DIAGNOSIS");
            modelBuilder.Entity<VitalSignsV>().ToView("V_HCD_VITAL_SIGNS");
            modelBuilder.Entity<LabV>().ToView("V_HCD_LAB");
            modelBuilder.Entity<LabResultV>().ToView("V_HCD_LAB_RESULT");
            modelBuilder.Entity<OrdersV>().ToView("V_HCD_ORDERS_VIEW");
            modelBuilder.Entity<OperationV>().ToView("V_HCD_OPERATION_VIEW");
            modelBuilder.Entity<ExamV>().ToView("V_HCD_EXAM_VIEW");
            modelBuilder.Entity<ExamReportV>().ToView("V_HCD_EXAM_REPORT_VIEW");

            modelBuilder.Entity<StaffDictV>().ToView(ViewsName.V_STAFF_DICT);
            modelBuilder.Entity<DeptDictV>().ToView(ViewsName.V_DEPT_DICT);

            modelBuilder.Entity<DrugDictV>().ToView(ViewsName.V_DRUG_DICT).HasNoKey();
            modelBuilder.Entity<ClinicItemDictV>().ToView(ViewsName.V_CLINIC_ITEM_DICT).HasNoKey();
            modelBuilder.Entity<DiagnosisDictV>().ToView(ViewsName.V_DIAGNOSIS_DICT).HasNoKey();
            modelBuilder.Entity<OperationDictV>().ToView(ViewsName.V_OPERATION_DICT).HasNoKey();
            modelBuilder.Entity<OrdersV>().ToView(ViewsName.V_HCD_ORDERS_VIEW).HasNoKey();
            modelBuilder.Entity<OperationV>().ToView(ViewsName.V_HCD_OPERATION_VIEW).HasNoKey();
            modelBuilder.Entity<ExamV>().ToView(ViewsName.V_HCD_EXAM_VIEW).HasNoKey();
            modelBuilder.Entity<ExamReportV>().ToView(ViewsName.V_HCD_EXAM_REPORT_VIEW).HasNoKey();
        }

        //Add DbSet properties for your entities...
        #region 定义视图
        public virtual DbSet<PatsDiagnosisV> PatsDiagnosisV {get;set;}
        public virtual DbSet<VitalSignsV> VitalSignsV {get;set;}
        public virtual DbSet<LabV> LabV {get;set;}
        public virtual DbSet<LabResultV> LabResultV {get;set;}
        public virtual DbSet<StaffDictV> StaffDictV { get; set; }
        public virtual DbSet<DeptDictV> DeptDictV { get; set; }
        public virtual DbSet<DrugDictV> DrugDictV { get; set; }
        public virtual DbSet<ClinicItemDictV> ClinicItemDictV { get; set; }
        public virtual DbSet<DiagnosisDictV> DiagnosisDictV { get; set; }
        public virtual DbSet<OperationDictV> OperationDictV { get; set; }
        public virtual DbSet<OrdersV> OrdersV { get; set; }
        public virtual DbSet<OperationV> OperationV { get; set; }
        public virtual DbSet<ExamV> ExamV { get; set; }
        public virtual DbSet<ExamReportV> ExamReportV { get; set; }
        #endregion
    }
}